package com.example.lte.repo;

import com.example.lte.entity.LoginLogEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lzh
 * @date 2020/11/5 - 19:06
 */
@Repository
public interface LoginLogRepo extends JpaSpecificationExecutor<LoginLogEntity>, JpaRepository<LoginLogEntity, Long> {

    @Transactional
    @Modifying(clearAutomatically = true)
    int deleteByReqTokenEquals(String reqToken);


    /**
     * 目标用户是否为今日已上线的用户
     * @param ids  userIds
     * @return  List<String>  目标中的今日上线用户
     */
    @Query(nativeQuery = true, value = "SELECT DISTINCT a.account " +
            "FROM login_log a,user b " +
            "WHERE a.account = b.account " +
            "AND to_days( a.add_time ) <= to_days( now( ) ) AND b.id IN ?1")
    List<String> findDistinctAccountOnlineTodayByUserIds(Long[] ids);

}
